package com.ruoyi.purchase.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.purchase.vo.DeliveryVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.purchase.domain.Delivery;
import com.ruoyi.purchase.service.IDeliveryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 发货单Controller
 * 
 * @author ruoyi
 * @date 2025-10-26
 */
@RestController
@RequestMapping("/purchase/delivery")
public class DeliveryController extends BaseController
{
    @Autowired
    private IDeliveryService deliveryService;


    /**
     * 确认发货
     */
    @PreAuthorize("@ss.hasPermi('purchase:delivery:confirm')")
    @Log(title = "发货单", businessType = BusinessType.UPDATE)
    @PutMapping("/confirm/{id}")
    public AjaxResult confirmDelivery(@PathVariable("id") Long id) {
        return deliveryService.confirmDelivery(id);
    }


    /**
     * 查询发货单列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:delivery:list')")
    @GetMapping("/listVO")
    public TableDataInfo listVO(Delivery delivery) {
        startPage();
        List<DeliveryVO> list = deliveryService.selectDeliveryVOList(delivery);
        System.out.println("发货单"+list);
        return getDataTable(list);
    }


    /**
     * 查询发货单列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:delivery:list')")
    @GetMapping("/list")
    public TableDataInfo list(Delivery delivery)
    {
        startPage();
        List<Delivery> list = deliveryService.selectDeliveryList(delivery);
        return getDataTable(list);
    }

    /**
     * 导出发货单列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:delivery:export')")
    @Log(title = "发货单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Delivery delivery)
    {
        List<Delivery> list = deliveryService.selectDeliveryList(delivery);
        ExcelUtil<Delivery> util = new ExcelUtil<Delivery>(Delivery.class);
        util.exportExcel(response, list, "发货单数据");
    }

    /**
     * 获取发货单详细信息
     */
    @PreAuthorize("@ss.hasPermi('purchase:delivery:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfoVO(@PathVariable("id") Long id) {
        return AjaxResult.success(deliveryService.selectDeliveryVOById(id));
    }
    /**
     * 获取发货单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('purchase:delivery:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return success(deliveryService.selectDeliveryById(id));
//    }

    /**
     * 新增发货单
     */
    @PreAuthorize("@ss.hasPermi('purchase:delivery:add')")
    @Log(title = "发货单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeliveryVO  deliveryVO)
    {
        System.out.println("发货单的数据"+deliveryVO);
        return toAjax(deliveryService.addDeliveryWithDetails(deliveryVO));
    }

    /**
     * 修改发货单
     */
    @PreAuthorize("@ss.hasPermi('purchase:delivery:edit')")
    @Log(title = "发货单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Delivery delivery)
    {
        return toAjax(deliveryService.updateDelivery(delivery));
    }

    /**
     * 删除发货单
     */
    @PreAuthorize("@ss.hasPermi('purchase:delivery:remove')")
    @Log(title = "发货单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deliveryService.deleteDeliveryByIds(ids));
    }
}
